package ${servicePkg};


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${poPkg}.${poName};
import ${qoPkg}.${qoName};
import ${toPkg}.ResultTo;
import ${mapperPkg}.${mapperName};
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class ${serviceName} {
    @Autowired
	private ${mapperName} ${alias}Mapper;

    /**
     * 根据ID查询对象信息
     * @param id
     */
    public ${poName} get${aliasCapFirst}(long id) {
		return ${alias}Mapper.selectById(id);
	}

    /**
     * 根据ID删除对象
     * @param id
     */
    public void del${aliasCapFirst}(long id) {
        int rs = ${alias}Mapper.deleteById(id);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "删除数据失败"));
    }

    /**
     * 根据ID集合批量删除对象
     * @param ids ID的List集合
     */
    public void del${aliasCapFirst}ByIds(List<Long> ids) {
        int rs = ${alias}Mapper.deleteBatchIds(ids);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "批量删除数据失败"));
    }

    /**
     * 新增对象
     * @param ${alias} 新增的PO对象
     */
    public void add${aliasCapFirst}(${poName} ${alias}) {
        int rs = ${alias}Mapper.insert(${alias});
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "新增数据失败"));
    }

    /**
     * 根据ID更新对象，对象的ID值必须传入
     * @param ${alias} 更新的PO对象，必传ID值
     */
    public void update${aliasCapFirst}(${poName} ${alias}) {
        int rs = ${alias}Mapper.updateById(${alias});
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "更新数据失败"));
    }

    /**
     * 根据条件查询记录数
     * @param ${alias}Qo 查询对象
     * @return int 记录总数
     */
    public int get${aliasCapFirst}Count(${qoName} ${alias}Qo) {
        return ${alias}Mapper.selectCount(getQw(${alias}Qo));
    }

    /**
     * 获取所有数据列表，不分页
     * @param ${alias}Qo 查询对象
     * @return List<${poName}>
     */
    public List<${poName}> get${aliasCapFirst}s(${qoName} ${alias}Qo) {
        return ${alias}Mapper.selectList(getQw(${alias}Qo));
    }

    /**
     * 获取所有数据列表，并分页
     * @param ${alias}Qo 查询对象
     * @return IPage<${poName}>
     */
    public IPage<${poName}> get${aliasCapFirst}AndPage(${qoName} ${alias}Qo) {
        return ${alias}Mapper.selectPage(new Page<>(${alias}Qo.getPageNo(), ${alias}Qo.getPageSize()), getQw(${alias}Qo));
    }

    /**
     * 构建Mybatis-Plus的查询条件对象。
     * TODO 此处需要自行根据需要构建
     * @param ${alias}Qo 查询对象
     * @return LambdaQueryWrapper<${poName}>
     */
    private LambdaQueryWrapper<${poName}> getQw(${qoName} ${alias}Qo) {
        return new QueryWrapper<${aliasCapFirst}Po>().lambda();
    }
}
